<?php
namespace app\admin\controller;

use think\Controll;
use \think\Db;
use \app\admin\model\Category as CatModel;
use \think\Request as Request;
class Category extends Base{
    
    
    public function show(){
        $model = new CatModel;
        $data = $model->getTree();
        $this->assign('data',$data);
        return $this->fetch();
    }
    
    public function add(){
        $model = new CatModel;
        $data = $model->select();
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function insert(Request $req){
        $cat_name = $req->post('cat_name');
        if(strlen($cat_name) == 0 || strlen($cat_name > 16)){
            return $this->error('分类名称不能为空/长度太长了');
        }else{
            $fid = $this->req->param('fid');
            $cat_order = $this->req->param('cat_order');
            $cat_content = $this->req->post('content');
            $cat_time = time();
            $data = [
                'cat_fid' => $fid,
                'cat_name' => $cat_name,
                'cat_order' => $cat_order,
                'cat_content' => $cat_content,
                'cat_create_time' => $cat_time
            ];
            $model = new CatModel;
            if($model->save($data)){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }


    public function get_tree($data, $fid=0, $self_id='cate_id', $key='f_id'){
        $tree = '';
        foreach($data as $k => $v){
            $b = $v->toArray();
            if($b[$key] == $fid){
                //父亲找到儿子
                $v[$key] = getTree($data, $b[$self_id]);
                $tree[] = $v;
                //unset($data[$k]);
            }
        }
        return $tree;
    }


    public function test(){
        $model = new CatModel;
        $tree = $model->cateTree();
        var_dump($tree);
        // var_dump($data->data);
    }

    public function edit(){
        $id = $this->req->get('id',0);
        if($id == 0 ){
            $this->error('error,system wrong!');
        }else{
            $model = new CatModel;
            $all = $model->select();
            $data = $model->where('cat_id',$id)->find();
            if(empty($data)){
                $this->error('Not found the category data');
            }else{
                $this->assign(['data'=>$data,'all'=>$all]);
                return $this->fetch();
            }
        }
    }

    public function update(){
        $id = $this->req->param('id');
        $post = $this->req->post();
        if(empty($id)){
            $this->error('error,the id was not input');
        }{
            $cat_name = $this->req->post('cat_name');
            if(strlen($cat_name) == 0 || strlen($cat_name > 16)){
                return $this->error('分类名称不能为空/长度太长了');
            }else{
                $fid = $this->req->param('fid');
                $fid = empty($fid) ? 0 : $fid;
                
                $cat_order = $this->req->param('cat_order');
                $cat_content = $this->req->param('editorValue');
                $cat_time = time();
                $data = [
                    'cat_id'          => $id,
                    'cat_fid'         => $fid,
                    'cat_name'        => $cat_name,
                    'cat_order'       => $cat_order,
                    'cat_content'     => $cat_content,
                    'cat_create_time' => $cat_time
                ];
                $model = new CatModel;
                if($model->update($data)){
                    $this->success('Edited Successful');
                }else{
                    $this->error('添加失败');
                }
            }
            
        }
        
    }

    public function getart(){
        $model = new CatModel;
        $data = $model->get(4)->article();
        var_dump($data);
    }


}
